package com.example.demo.trade;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.core.env.Environment;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

@SpringBootApplication
@EnableDiscoveryClient
public class StartTradeApplication {

    private static final Logger LOG = LoggerFactory.getLogger(StartTradeApplication.class);

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(StartTradeApplication.class);
        Environment env = app.run(args).getEnvironment();
        LOG.info("启动成功！！");
        // 限流规则
        //        initFlowRules();
    }

    private static void initFlowRules() {

        List<FlowRule> ruleList = new ArrayList<>();

        //        FlowRule rule = new FlowRule();
        //        rule.setResource("TRADE_TEST_SERVICE");
        //        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //        rule.setCount(11);
        //        ruleList.add(rule);

        FlowRule rule1 = new FlowRule();
        rule1.setResource("TRADE_TEST_CONTROLLER");
        rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule1.setCount(6);
        ruleList.add(rule1);

        FlowRuleManager.loadRules(ruleList);

        System.out.println("规则初始化成功！！！");
    }

}
